﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CompanyBussiness;
using System.Configuration;

namespace Website.customer.Control
{
    public partial class cListParentCategoryProduct : System.Web.UI.UserControl
    {
        HtmlMeta metaTag;
        NewsManagement newsMana = new NewsManagement();
        ProductManagement proMana = new ProductManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        TurnOverService overServices = new TurnOverService();
        CompanyBussiness.Product pro = new Product();
        CompanyBussiness.Language language;
        CheckValid cv = new CheckValid();
        public int CurPage = 1;
        int TotalRow;
        int TotalPage;
        int StartRow = 0;
        int typeProductIDs;
        string modulCode;
        Metadata meta;

        protected void Page_Load(object sender, EventArgs e)
        {
            modulCode = Request.QueryString["Mod"];
            //loadModul();

            string abc = Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString();
            typeProductIDs = int.Parse(Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString());

            if (Request.Cookies[CONST.COOKIES_LANGUAGE] == null)
            {
                Session[CONST.SESSION_LANGUAGE] = "";
                language = languageMana.GetLanguageByCode("vi-VN");
            }
            else
            {
                Session[CONST.SESSION_LANGUAGE] = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
                language = languageMana.GetLanguageByID(int.Parse(Session[CONST.SESSION_LANGUAGE].ToString()));
            }

            //if (!Page.IsPostBack)
            //{

            typeProductIDs = int.Parse(Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString());
            lbNameCategory.Text = proMana.GetTypeProductByID(typeProductIDs) != null ? proMana.GetTypeProductByID(typeProductIDs).TypeProductName : "";
            this.LoadDataList(int.Parse(ConfigurationManager.AppSettings["NumberPostInPage"]));

            meta = newsMana.GetMetadataByTypeProductID(typeProductIDs);
            if (meta != null)
            {
                //ghi metadata
                metaTag = new HtmlMeta();
                metaTag.Name = "keywords";
                metaTag.Content = meta.MetaKeyword;
                Page.Header.Controls.Add(metaTag);

                metaTag = new HtmlMeta();
                metaTag.Name = "description";
                metaTag.Content = meta.MetaDesciption;
                Page.Header.Controls.Add(metaTag);

                Page.Header.Title = meta.Title;
            }

            //}
        }


        protected void LoadDataList(int take)
        {

            try
            {
                typeProductIDs = int.Parse(Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString());

                if (Request["Page"] == null)
                {
                    CurPage = 1;
                }
                else
                {
                    CurPage = Convert.ToInt32(Request["Page"]);
                }

                //dropChoicePage.SelectedValue = CurPage.ToString();

                if (CurPage > 1)
                {
                    StartRow = (CurPage - 1) * 10;
                }
                else
                {
                    StartRow = 0;
                }


                dataListParentTypeProduct.DataSource = proMana.GetParentTypeProduct(language.LanguagesID, true, take, StartRow);
                dataListParentTypeProduct.DataBind();
                TotalRow = proMana.GetParentTypeProductPageCount(language.LanguagesID, true);




                if (TotalRow == 0)
                {
                    lbNameCategory.Text = languageMana.GetTagValueByLanguageIDandTagName("NotfoundNews", language.LanguagesID);
                    pt.Visible = false;
                    return;
                }
                else
                {
                    pt.Visible = true;
                }

                this.Pagination(take);
            }
            catch (Exception)
            {

            }
        }

        protected void Pagination(int take)
        {
            try
            {
                typeProductIDs = int.Parse(Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString());

                if (TotalRow % 4 == 0)
                    TotalPage = TotalRow / 4;
                else
                    TotalPage = (TotalRow / 4) + 1;



                if (CurPage > 1)
                {

                    //lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(CurPage - 1) + "&Mod=MPRODUCT";
                    lnkPrev.NavigateUrl = "~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + (CurPage - 1) + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "-")) + ".html";
                }
                if (CurPage < TotalPage && CurPage > 1)
                {

                    //lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(CurPage + 1) + "&Mod=MPRODUCT";
                    lnkNext.NavigateUrl = "~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + (CurPage + 1) + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "-")) + ".html";
                }


                if (CurPage == 1 && TotalPage == 1)
                {
                    lnkNext.Text = "";
                    lnkPrev.Text = "";
                    lnkNext.Visible = false;
                    lnkPrev.Visible = false;
                }
                else if (CurPage == 1 && TotalPage > 1)
                {
                    lnkPrev.Text = "";
                    lnkPrev.Visible = false;
                }
                else if (1 < CurPage && CurPage < TotalPage)
                {

                }
                else if (CurPage == TotalPage)
                {
                    lnkNext.Text = "";
                    lnkNext.Visible = false;
                }
            }
            catch (Exception)
            {

            }
        }


        protected void imgType_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                Session[CONST.SESSION_TYPE_PRODUCT] = Convert.ToInt32(e.CommandArgument);
                try
                {

                    Response.Redirect("~/Hem/san-pham-dich-vu/" + Session[CONST.SESSION_TYPE_PRODUCT].ToString() + "/" + modulCode + "/" + overServices.ConvertString(proMana.GetNametypeByID(int.Parse(Session[CONST.SESSION_TYPE_PRODUCT].ToString())).Replace(" ", "-")) + ".html");
                }
                catch (Exception)
                {

                }
            }
        }

        protected void lnkDetail_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                Session[CONST.SESSION_TYPE_PRODUCT] = Convert.ToInt32(e.CommandArgument);
                try
                {

                    Response.Redirect("~/Hem/san-pham-dich-vu/" + Session[CONST.SESSION_TYPE_PRODUCT].ToString() + "/" + modulCode + "/" + overServices.ConvertString(proMana.GetNametypeByID(int.Parse(Session[CONST.SESSION_TYPE_PRODUCT].ToString())).Replace(" ", "-")) + ".html");
                }
                catch (Exception)
                {

                }
            }
        }

        protected void dataListParentTypeProduct_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
            {


                ImageButton imgButton = e.Item.FindControl("imgType") as ImageButton;

                if (imgButton.ImageUrl == null || imgButton.ImageUrl.ToString() == string.Empty)
                {
                    imgButton.Visible = true;
                }

            }
        }



    }
}